<?php
/**
 * @description 众包审核
 * @author 陈武<chenwu@myhexin.com>
 * @history
 *         2020/4/21 22:52 create
 */
namespace Home\Model;
class AuditModel
{
    /**
     * @description 审核提交
     * @param string $id 记录ID
     * @param int $status 审核状态
     * @return bool|mixed
     */
    public function apply($id, $status)
    {
        $record = $this->findOne($id);
        if ($record) {
            $res = $this->update($id, $status);
            if ($status == 2) {
                if ($res) {
                    $relation = new RelationModel();
                    $record = $this->findOne($id);
                    if ($record['method'] == 1) {
                        $data = [
                            'concept_id' => $record['concept_id'],
                            'concept_name' => $record['concept_name'],
                            'code' => $record['code'],
                            'code_name' => $record['code_name'],
                            'explain' => $record['explain'],
                            'best' => $record['author'],
                            'ctime' => time(),
                            'rtime' => time()
                        ];
                        $res = $relation->insert($data);
                    } else {
                        $res = $relation->del(['code' => $record['code'], 'concept_id' => $record['concept_id']]);
                    }
                }
            }
            return $res;
        } else {
            return false;
        }
    }

    /**
     * @description 状态更新
     * @param int $id 记录ID
     * @param int $status 状态值
     * @return bool
     */
    public function update($id, $status)
    {
        return M('concept_stock_pool')->where(['id' => $id])->save(['status' => $status, 'rtime' => time()]);
    }

    /**
     * @description 查询单条记录
     * @param int $id 记录ID
     * @return array|mixed
     */
    public function findOne($id)
    {
        return M('concept_stock_pool')->where(['id' => $id])->find();
    }

    /**
     * @description 搜索
     * @param int $page
     * @param int $size
     * @param array $where
     * @return mixed
     */
    public function search($page = 1, $size = 10, $where = [])
    {
        return M('concept_stock_pool')->page($page)->limit($size)->where($where)->select();
    }

    /**
     * @description 总条数
     * @param array $where
     * @return int
     */
    public function total($where)
    {
        $result = M('concept_stock_pool')->field('id')->where($where)->select();
        return $result?count($result):0;
    }
}